﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Cn.Edwon.Web.Support;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using System.Reflection;
using Cn.Cellula.Biz;
using Cn.Cellula.Model;
using Cn.Edwon.Web;
using System.Collections.Generic;
using System.Text;
using Cn.Edwon.Web.Validation;
using Cn.Cellula.Model.Product;
using Cn.Cellula.Client;
namespace Cn.Cellula.Website.Admin.Product {
    public partial class AddSaleInfo : PageSupport {
        IProductBiz biz = BizFactory.Get<IProductBiz>();
        ProductEditBiz peb = new ProductEditBiz();
        protected void Page_Load(object sender, EventArgs e) {
            if (!Page.IsPostBack) {
                InitForm();
            }
        }
        private void InitForm() {
            ProductActor pa = peb.PopFlow(Session);
            if (pa != null) {
                this.lab_CategoryName.Text = BizFactory.Get<ICategoryBiz>().LoadCategoryPath(this.OwnerKey, CodeTypeDef.Category.Product, pa.CategoryKey, " / ");
                this.hid_CategoryKey.Value = pa.CategoryKey;
                try {
                    TProductEO peo = biz.Load<TProductEO>(pa.ProductKey);
                    this.lab_ProductName.Text = peo.ProductName;
                    this.hid_ProductKey.Value = pa.ProductKey;
                } catch {
                    SiteInfo.Send(Page, "错误", "没有指定有效的产品");
                    return;
                }
            } else {
                SiteInfo.Send(Page, "错误", "没有选择有效的产品分类");
                return;
            }
        }
        protected void btn_Finish_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_CostPrice.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            }
            TProductSaleEO ps = new TProductSaleEO();
            try {
                ps.CostPrice = Convert.ToDecimal(this.txt_CostPrice.Text);
            } catch {
                ps.CostPrice = 0;
            }
            try {
                ps.GroupPrice = Convert.ToDecimal(this.txt_GroupPrice.Text);
            } catch {
                ps.GroupPrice = 0;
            }
            try {
                ps.PreferentialPrice = Convert.ToDecimal(this.txt_PreferentialPrice.Text);
            } catch {
                ps.PreferentialPrice = 0;
            }
            try {
                ps.ProductCount = Convert.ToInt32(this.txt_ProductNum.Text);
            } catch {
                ps.ProductCount = 0;
            }
            try {
                ps.RetailPrice = Convert.ToDecimal(this.txt_RetailPrice.Text);
            } catch {
                ps.RetailPrice = 0;
            }
            try {
                ps.TransCost = Convert.ToDecimal(this.txt_TransCost.Text);
            } catch {
                ps.TransCost = 0;
            }
            ps.CreateDate = DateTime.Now;
            ps.UpdateDate = DateTime.Now;
            ps.ProductKey = this.hid_ProductKey.Value;
            try {
                biz.Save<TProductSaleEO>(ps);
                peb.GoNext(Request, Response);
            } catch (Exception ex) {
                log.Error(ex.Message);
                SiteInfo.Send(Page, TxtDef.ERROR, "保存数据时发生错误");
                return;
            }
        }
    }
}
